{% extends "../theme/bage.html" %}
{% block head %}
<title>{{ _('App') }}</title>
<meta name="description" content="">
<meta name="keywords" content="">
{% end %}
{% block typebody %}
<h1 class="hden">{{ _('App') }}</h1>
<div class="well well-sm">
    {{ _('Maintenance by scientists and engineers, focusing on online computing, the realization of the professional formula online computing functions.') }}
</div>
<div class="row">
    <div class="col-sm-9">
        <div class="row">
            <div class="col-sm-4">
                <div class="panel panel-black"> {% module app_catalog_of('s100', slug = True,count=True) %}</div>
                <div class="panel panel-black"> {% module app_catalog_of('s200', slug = True,count=True) %}</div>
                <div class="panel panel-black"> {% module app_catalog_of('s300', slug = True,count=True) %}</div>
            </div>
            <div class="col-sm-4">
                <div class="panel panel-black"> {% module app_catalog_of('s400', slug = True,count=True) %}</div>
                <div class="panel panel-black"> {% module app_catalog_of('s500', slug = True,count=True) %}</div>
                <div class="panel panel-black"> {% module app_catalog_of('s600', slug = True,count=True) %}</div>
            </div>
            <div class="col-sm-4">
                <div class="panel panel-black"> {% module app_catalog_of('s700', slug = True,count=True) %}</div>
                <div class="panel panel-black"> {% module app_catalog_of('s800', slug = True,count=True) %}</div>
                <div class="panel panel-black"> {% module app_catalog_of('s900', slug = True,count=True) %}</div>
            </div>
        </div>
    </div>
    <div class="col-sm-3">
        <div class="panel panel-black">
            <div class="panel-heading">
                {{ _('Recent calculation') }}
            </div>
            <ul class="list-group">
                {% module app_recent_used('s', 8, userinfo = userinfo) %}
            </ul>
        </div>
        <div class="panel panel-black">
            <div class="panel-heading">
                {{ _('Application classification') }}
            </div>
            <div class="panel-body">
                <ul class="list-group">
                    {% module app_menu('s', 20) %}
                </ul>
            </div>
        </div>
    </div>
</div>
<script>
    $.ready()
    {
        // Get all the keys from document
        var keys = document.querySelectorAll('#calculator span');
        var operators = ['+', '-', 'x', '÷'];
        var decimalAdded = false;

        // Add onclick event to all the keys and perform operations
        for (var i = 0; i < keys.length; i++) {
            keys[i].onclick = function (e) {
                // Get the input and button values
                var input = document.querySelector('.screen');
                var inputVal = input.innerHTML;
                var btnVal = this.innerHTML;

                // Now, just append the key values (btnValue) to the input string and finally use javascript's eval function to get the result
                // If clear key is pressed, erase everything
                if (btnVal == 'C') {
                    input.innerHTML = '';
                    decimalAdded = false;
                }

                // If eval key is pressed, calculate and display the result
                else if (btnVal == '=') {
                    var equation = inputVal;
                    var lastChar = equation[equation.length - 1];

                    // Replace all instances of x and ÷ with * and / respectively. This can be done easily using regex and the 'g' tag which will replace all instances of the matched character/substring
                    equation = equation.replace(/x/g, '*').replace(/÷/g, '/');

                    // Final thing left to do is checking the last character of the equation. If it's an operator or a decimal, remove it
                    if (operators.indexOf(lastChar) > -1 || lastChar == '.')
                        equation = equation.replace(/.$/, '');

                    if (equation)
                        input.innerHTML = eval(equation);

                    decimalAdded = false;
                }

                // Basic functionality of the calculator is complete. But there are some problems like
                // 1. No two operators should be added consecutively.
                // 2. The equation shouldn't start from an operator except minus
                // 3. not more than 1 decimal should be there in a number

                // We'll fix these issues using some simple checks

                // indexOf works only in IE9+
                else if (operators.indexOf(btnVal) > -1) {
                    // Operator is clicked
                    // Get the last character from the equation
                    var lastChar = inputVal[inputVal.length - 1];

                    // Only add operator if input is not empty and there is no operator at the last
                    if (inputVal != '' && operators.indexOf(lastChar) == -1)
                        input.innerHTML += btnVal;

                    // Allow minus if the string is empty
                    else if (inputVal == '' && btnVal == '-')
                        input.innerHTML += btnVal;

                    // Replace the last operator (if exists) with the newly pressed operator
                    if (operators.indexOf(lastChar) > -1 && inputVal.length > 1) {
                        // Here, '.' matches any character while $ denotes the end of string, so anything (will be an operator in this case) at the end of string will get replaced by new operator
                        input.innerHTML = inputVal.replace(/.$/, btnVal);
                    }

                    decimalAdded = false;
                }

                // Now only the decimal problem is left. We can solve it easily using a flag 'decimalAdded' which we'll set once the decimal is added and prevent more decimals to be added once it's set. It will be reset when an operator, eval or clear key is pressed.
                else if (btnVal == '.') {
                    if (!decimalAdded) {
                        input.innerHTML += btnVal;
                        decimalAdded = true;
                    }
                }

                // if any other key is pressed, just append it
                else {
                    input.innerHTML += btnVal;
                }

                // prevent page jumps
                e.preventDefault();
            }
        }
    }
</script>
{% end %}

